rm(list = ls())

##

library(tidyverse)

## Get data

bt <- read_rds('data/data_federal.rds') %>% 
  filter(year == 2013) %>% 
  dplyr::select(ags, matches('refugees_2017'), 
                treated, pop_dec_09) %>%
  filter(!pop_dec_09 > 20000)

## Plot

bt$pop_2009_bin <- cut(bt$pop_dec_09, 
                            breaks = seq(0, 20000, 2000),
                     dig.lab = 10)

## Population change by bin

bins <- bt %>% group_by(pop_2009_bin) %>% 
  summarise(m = mean(refugees_2017, na.rm = T) * 100) %>% 
  ungroup()

# Figure A.22: refugee share by population ----

p1 <- ggplot(bins, aes(pop_2009_bin, m)) +
  geom_bar(stat = 'identity', fill = 'grey93', color = 'black',
           width = 0.6) +
  theme_bw() +
  geom_vline(xintercept = 5.5, linetype = 'dotted') +
  ylab('Refugee share of\ntotal population (%, 2017)') +
  xlab('Binned pre-census population (2009)') +
  theme(axis.text.x = element_text(angle = 90, 
                                   vjust = 0.5, 
                                   hjust=1))
p1
